Skip to content

Files

Latest commit

a67e58e · Aug 30, 2020

History

History
46 lines (36 loc) · 1.04 KB

剑指 Offer 27. 二叉树的镜像.md

File metadata and controls

46 lines (36 loc) · 1.04 KB

题目位置

https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/


题解

B站视频讲解 https://www.bilibili.com/video/BV1P54y1v7AE

    /**
     * 理解:除了叶子节点,每个节点左右互换
     * 
     * @author 小道仙
     * @date 2020/8/29
     */
    TreeNode tmp = new TreeNode(-1);
    public TreeNode mirrorTree(TreeNode root) {
        if (root == null){
            return root;
        }
        TreeNode head = new TreeNode(-1);
        head.left = root;
        change(root);
        return head.left;
    }

    private void change(TreeNode root){
        if (root == null){
            return;
        }
        if (root.left == null && root.right == null){
            return;
        }
        tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        if (root.left != null) change(root.left);
        if (root.right != null) change(root.right);
    }